// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
//var net = require('Connection');
var gt = require("protocol");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        _scheduler: null,
    },

    // LIFE-CYCLE CALLBACKS:
    queue() {
        if (this.sendMsgCache.length <= 0) {
            return;
        }
        var t = this.sendMsgCache.shift();
        console.log(t);
    },

    onLoad() {
        App.socket.registerMsgListener(gt.GC_LOGIN, this, this.onRcvLogin);
        App.socket.registerMsgListener(gt.GC_LOGIN_GATE, this, this.onRcvLoginGate);
        App.socket.registerMsgListener(gt.GC_LOGIN_SERVER, this, this.onRcvLoginServer);
        App.socket.registerMsgListener(gt.GC_ROOM_CARD, this, this.onRcvRoomCard);
        // App.socket.registerMsgListener(gt.GC_MARQUEE, this, this.onRcvMarquee)
        // App.socket.registerMsgListener(gt.GC_IS_ACTIVITIES, this, this.onRecvIsActivities)
        this.login();
    },

    login: function () {
        // var openid = sys.params.openid || '', unionid = sys.params.unionid, accessToken = sys.params.accessToken || '';
        // if (!unionid) {
        //     location.href = sys.indexUrl;
        //     return;
        // }
        var msgToSend = {};
        msgToSend.m_accessToken = 'accessToken';
        msgToSend.m_refreshToken = '';
        msgToSend.m_msgId = gt.CG_LOGIN;
        msgToSend.m_isWebUser = 1;
        msgToSend.m_openId = 'test1';
        msgToSend.m_nike = 'nickname';
        msgToSend.m_sign = 123987;
        msgToSend.m_plate = 'local';//unionid ? 'wechat' : 'local';
        msgToSend.m_severID = 30001;
        msgToSend.m_uuid = msgToSend.m_openId;
        msgToSend.m_sex = -1;//sys.params.sex || -1;
        msgToSend.m_nikename = 'nickname';
        msgToSend.m_imageUrl = '';
        App.socket.send(msgToSend);

        //msgToSend.m_md5 = '';//new md5().hex_md5(openid + accessToken + unionid + Const.SECRET_KEY);
        // App.socket.sendMessage(msgToSend);
        // console.log('send ...', gt.CG_LOGIN);
    },

    onRcvLogin: function (msg) {
        App.socket.close();
        var gateIp = msg.m_gateIp;
        var gatePort = - msg.m_gatePort;
        var url = 'ws://' + gateIp + ':' + gatePort;
        console.log(url)
        App.socket.init(url);

        var msgToSend = {};
        msgToSend.m_msgId = gt.CG_LOGIN_GATE;
        msgToSend.m_strUserUUID = 'test1';
        App.socket.send(msgToSend);

        //cc.director.loadScene("Hall");
    },
    onRcvLoginGate: function (msg) {

    },
    onRcvLoginServer: function () {

    },
    onRcvRoomCard: function () {

    },
    start() {

    },

    update(dt) {
        //this.queue();
    },
});
